package com.sphd.emanu.fragment.vip;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.vip.VipActivity;
import com.sphd.emanu.activity.vipdetails.VipdetailsActivity;
import com.sphd.emanu.adapter.VipAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.interfaces.MemberCountCallBack;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.login.Org;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.vip.Member;
import com.wzutils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 会员界面包含三种状态
 *
 * @Author Hu
 * @Time 2017/6/26 -- 19:37.
 * @Email 87683202@qq.com
 */
public class VipFragment extends MVPBaseFragment<VipContract.View, VipPresenter> implements VipContract.View {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    VipAdapter vipAdapter;
    Bundle bundle;
    int type;
    User user;
    MemberCountCallBack memberCountCallBack;
    Intent intent;

    public void setMemberCountCallBack(VipActivity memberCountCallBack) {
        this.memberCountCallBack = memberCountCallBack;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_vip;
    }

    @Override
    protected void initView() {
        intent = new Intent();
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        L.e(type + "---------------");
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        setRefresh(mySwf);
        initAdapter();
        initEvent();


    }

    private void initAdapter() {
        setRecyWithDecoration(myRecycler);
        vipAdapter = new VipAdapter(R.layout.item_fragment_vip, new ArrayList<Member>());
       vipAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(vipAdapter);
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());

        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Member member = (Member) adapter.getItem(position);
                bundle.putSerializable(Constant.MEMBER_ENTITY, member);
                intent.putExtras(bundle);
                intent.setClass(context, VipdetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        addData();

    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectMember(user.getOid(), type);
    }

    @Override
    public void recBean(Org org1) {
        dissProgressDialog();
        memberCountCallBack.callBackCount(org1.getMemberCount());
        vipAdapter.setNewData(org1.getMembers());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
